
;window.ServiceManager = (function (helper) {
  var
    isInit = false;

  /* <community> */
  var getCmtBlog = function (eventname, params, id, callback) {
    helper.request(
      eventname,
      params,
      'get',
      'blog/' + id + '.json',
      null,
      callback
    );
  };
  /* </community> */

  /* <projects> */
  var getPrjTask = function (eventname, params, id, callback) {
    helper.request(
      eventname,
      params,
      'get',
      'project/task/' + id + '.json',
      null,
      callback
    );
  };

  var getPrjTasks = function (eventname, params, id, callback) {
    helper.request(
      eventname,
      params,
      'get',
      'project/' + id + '/task.json',
      null,
      callback
    );
  };

  var getPrjDiscussion = function (eventname, params, id, callback) {
    helper.request(
      eventname,
      params,
      'get',
      ['project/message/' + id + '.json', 'project/message/' + id + '/comment.json'],
      null,
      callback
    );
  };

  var getPrjDiscussions = function (eventname, params, id, callback) {
    helper.request(
      eventname,
      params,
      'get',
      'project/' + id + '/message.json',
      null,
      callback
    );
  };

  var getPrjProjects = function (eventname, params, option, callback) {
    helper.request(
      eventname,
      params,
      'get',
      'project' + (option ? '/' + option : '') + '.json',
      null,
      callback
    );
  };

  var getPrjSelfProjects = function (eventname, params, callback) {
    return getPrjProjects(eventname, params, '@self', callback);
  };

  var getPrjFollowProjects = function (eventname, params, callback) {
    return getPrjProjects(eventname, params, '@follow', callback);
  };

  var addPrjDiscussionComment = function (eventname, params, id, data, callback) {
    if (!data.parentid) {
      data.parentid = '00000000-0000-0000-0000-000000000000';
    }

    helper.request(
      eventname,
      params,
      'post',
      'project/message/' + id + '/comment.json',
      data,
      callback
    );
    return true;
  };

  var updatePrjDiscussionComment = function (eventname, params, id, data, callback) {
    if (!data.parentid) {
      data.parentid = '00000000-0000-0000-0000-000000000000';
    }
    if (!data.hasOwnProperty('text') && data.hasOwnProperty('content')) {
      data.text = data.content;
    }

    helper.request(
      eventname,
      params,
      'put',
      'project/comment/' + id + '.json',
      data,
      callback
    );
    return true;
  };

  var removePrjDiscussionComment = function (eventname, params, id, callback) {
    helper.request(
      eventname,
      params,
      'delete',
      'project/comment/' + id + '.json',
      null,
      callback
    );
    return true;
  };
  /* <projects> */

  return {
    test    : helper.test,
    init    : helper.init,
    bind    : helper.bind,
    exec    : helper.exec,
    login   : helper.login,
    logged  : helper.logged,

    getCmtBlog : getCmtBlog,

    getPrjTask            : getPrjTask,
    getPrjTasks           : getPrjTasks,
    getPrjDiscussion      : getPrjDiscussion,
    getPrjDiscussions     : getPrjDiscussions,
    getPrjProjects        : getPrjProjects,
    getPrjSelfProjects    : getPrjSelfProjects,
    getPrjFollowProjects  : getPrjFollowProjects,

    addPrjDiscussionComment     : addPrjDiscussionComment,
    updatePrjDiscussionComment  : updatePrjDiscussionComment,
    removePrjDiscussionComment  : removePrjDiscussionComment
  };
})(ServiceHelper);
